package com.mm.cloud.sys.controller;

import com.mm.cloud.common.core.result.Result;
import com.mm.cloud.common.security.annotation.PreAuthorize;
import com.mm.cloud.sys.dto.form.PermissionDTO;
import com.mm.cloud.sys.dto.query.QueryPermissionDTO;
import com.mm.cloud.sys.service.IPermissionService;
import com.mm.cloud.sys.vo.PermissionVO;
import com.mm.cloud.sys.vo.RouterVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 资源基础信息 前端控制器
 * </p>
 *
 * @author mr
 * @since 2020-09-15
 */
@Tag(name = "资源基础信息")
@RestController
@AllArgsConstructor
@RequestMapping("/sys/permission")
public class PermissionController {

    /**
     * 资源基础信息
     */
    private final IPermissionService permissionService;

    @Operation(summary = "查询所有资源【mr】", description = "查询所有资源")
    @PostMapping("/getPermissionList")
    @PreAuthorize(hasAuthorize = "ROLE_PERMISSION_LIST")
    public Result<List<PermissionVO>> getPermissionList(@RequestBody QueryPermissionDTO dto) {
        return permissionService.getPermissionList(dto);
    }

    @Operation(summary = "新增资源【mr】", description = "新增资源")
    @PostMapping("/savePermission")
    @PreAuthorize(hasAuthorize = "ROLE_PERMISSION_ADD")
    public Result<String> savePermission(@RequestBody PermissionDTO dto) {
        return permissionService.savePermission(dto);
    }

    @Operation(summary = "修改资源【mr】", description = "修改资源")
    @PostMapping("/updatePermission")
    @PreAuthorize(hasAuthorize = "ROLE_PERMISSION_EDIT")
    public Result<String> updatePermission(@RequestBody PermissionDTO dto) {
        return permissionService.updatePermission(dto);
    }

    @Operation(summary = "删除资源【mr】", description = "删除资源")
    @PostMapping("/deletePermission/{permissionId}")
    @PreAuthorize(hasAuthorize = "ROLE_PERMISSION_DELETE")
    public Result<String> deletePermission(@PathVariable("permissionId") Long permissionId) {
        return permissionService.deletePermission(permissionId);
    }

    @Operation(summary = "初始化菜单【mr】", description = "初始化菜单")
    @Parameter(name = "platformType", description = "平台类型 1-平台管理 2-商城管理", required = true)
    @PostMapping("/init/{platformType}")
    public Result<List<RouterVO>> init(@PathVariable("platformType") Byte platformType) {
        return permissionService.init(platformType);
    }

}

